$(function(){
  var filechooser = document.getElementById('filechooser');
  var previewer = document.getElementById('previewer');

  filechooser.onchange = function() {
      console.log('suck')
      var files = this.files;
      var file = files[0];

      // 接受 jpeg, jpg, png 类型的图片
      if (!/\/(?:jpeg|jpg|png|gif)/i.test(file.type)) return;

      var reader = new FileReader();

      reader.onload = function() {
          var result = this.result;
          console.log(result.length)
          previewer.src = result;
          var form = new FormData();
          var blob = b64toBlob(result);
          form.append('file',file)
          $.ajax({
            url:'/dev/fuck',
            type:'post',
            cache: false,
            contentType: false,
            processData: false,
            data:form
          }).done(function(data){
              console.log(data)
              // 清空图片上传框的值
              filechooser.value = '';
          })

      };
      reader.readAsDataURL(file);
  };

})
function b64toBlob(dataURI){
  // 解码base64
  var byteString = atob(dataURI.split(',')[1]);
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  // 类型数组
  var ia = new Uint8Array(byteString.length);
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }
  return new Blob([ia], {
      type: mimeString
  });
}
